"""
推荐系统示例内容数据填充脚本
"""

from sqlalchemy.orm import Session
from src.models.user_preference import ContentItem
import uuid
from datetime import datetime
import logging

logger = logging.getLogger(__name__)

def create_sample_content_items(db: Session):
    """创建示例内容项数据"""
    sample_content_items = [
        {
            "title": "机器学习基础入门",
            "description": "机器学习的基本概念、算法和应用案例介绍",
            "content_type": "document",
            "tags": ["机器学习", "人工智能", "算法"],
            "category": "教程",
            "content_metadata": {"difficulty": "初级", "estimated_time": "30分钟"}
        },
        {
            "title": "深度学习实战",
            "description": "使用TensorFlow和PyTorch进行深度学习模型开发",
            "content_type": "tutorial",
            "tags": ["深度学习", "神经网络", "TensorFlow", "PyTorch"],
            "category": "实战",
            "content_metadata": {"difficulty": "高级", "estimated_time": "120分钟"}
        },
        {
            "title": "自然语言处理技术",
            "description": "NLP领域的最新进展和技术实现",
            "content_type": "article",
            "tags": ["自然语言处理", "NLP", "语言模型"],
            "category": "技术文章",
            "content_metadata": {"difficulty": "中级", "estimated_time": "45分钟"}
        },
        {
            "title": "数据可视化最佳实践",
            "description": "如何使用Python进行有效的数据可视化",
            "content_type": "guide",
            "tags": ["数据可视化", "Python", "Matplotlib", "Seaborn"],
            "category": "指南",
            "content_metadata": {"difficulty": "中级", "estimated_time": "60分钟"}
        },
        {
            "title": "云计算架构设计",
            "description": "大规模分布式系统的架构设计原则和模式",
            "content_type": "document",
            "tags": ["云计算", "架构设计", "分布式系统"],
            "category": "架构",
            "content_metadata": {"difficulty": "高级", "estimated_time": "90分钟"}
        },
        {
            "title": "网络安全防护策略",
            "description": "企业级网络安全防护的策略和实施方法",
            "content_type": "article",
            "tags": ["网络安全", "信息安全", "防护策略"],
            "category": "安全",
            "content_metadata": {"difficulty": "中级", "estimated_time": "50分钟"}
        },
        {
            "title": "DevOps实践指南",
            "description": "DevOps流程、工具和最佳实践详解",
            "content_type": "tutorial",
            "tags": ["DevOps", "CI/CD", "Docker", "Kubernetes"],
            "category": "运维",
            "content_metadata": {"difficulty": "中级", "estimated_time": "75分钟"}
        },
        {
            "title": "移动应用开发框架",
            "description": "主流移动应用开发框架对比和选型建议",
            "content_type": "guide",
            "tags": ["移动开发", "React Native", "Flutter", "iOS", "Android"],
            "category": "开发",
            "content_metadata": {"difficulty": "中级", "estimated_time": "60分钟"}
        }
    ]
    
    created_items = []
    for item_data in sample_content_items:
        try:
            content_item = ContentItem(**item_data)
            db.add(content_item)
            created_items.append(content_item)
        except Exception as e:
            logger.error(f"创建内容项失败: {e}")
    
    try:
        db.commit()
        logger.info(f"已创建 {len(created_items)} 个示例内容项")
        return created_items
    except Exception as e:
        db.rollback()
        logger.error(f"提交内容项数据失败: {e}")
        return []


def seed_recommendation_data(db: Session):
    """填充推荐系统示例数据"""
    try:
        # 创建示例内容项
        content_items = create_sample_content_items(db)
        return len(content_items)
    except Exception as e:
        logger.error(f"填充推荐系统数据失败: {e}")
        return 0